function [W, cost] = linearRegressionModel(F,Y, MAXIte, alpha, lambda )
%linear regression model for features F, target Y, number of iterations
%MAXIte, learning rate alpha and regularization factor lambda
%return: Optimum parameters W and a vector with the cost of each iteration
    
    

    %number of examples
    M = size(F,1);
    
    %number of features
    N = size(F,2);

    % insert the first column as one to have the independent term in the
    % linear regression
    F = [ones(M,1) F];
    
    %initialize the parameters as a column vector of n + 1 elements 
    W = zeros(N+1, 1);
    
%     %DEBUG: W Optimum
%    Ws = [34.59; 7.556; 4.2476; 19.717; 29.217; 34.805];
%     W=Ws;
  
    %create a vector to store the cost function in each iteration
    cost = zeros(MAXIte,1);

    %loop for each iterarion
    for i = 1:MAXIte
        
        %calculate the hypothesis
        H = F * W;
        
        %calculate the cost
        cost(i,1) = (1/(2*M)) .* ( sum((H - Y) .^ 2) + (lambda * sum( W(2:end,1) .^ 2)));

        %adjust parameters with gradient formula
        W = W - alpha .* (((1/M) .* (F' * (H - Y))) + ((lambda/M) .* [0;W(2:end,1)]));   
        
    end;

%     %DEBUG
%     sprintf('dist %0.2f' ,sqrt(sum((Ws - W) .^ 2)))
end